from dao.user_dao import UserDao
from model.user import User
from model.company import Company
from model.job import Job
from model.user_deliver_status import UserDeliverStatus
from model.user_resume import UserResume


class UserService:
    def __init__(self, session):
        self.session = session

    def add_user(self, user):
        dao = UserDao(self.session)
        dao.add(user)

    def get_by_tel(self, tel):
        dao = UserDao(self.session)
        return dao.get_by_tel(tel)

    def get_by_id(self, user_id):
        dao = UserDao(self.session)
        return dao.get_by_id(user_id)

    def get_user_and_resume_by_id(self, user_id):
        q = self.session.query(User, User.user_id, User.wx_nick_name,
                               User.tel,
                               User.email,
                               User.header_img,
                               User.openid,
                               User.sec_pub_tel,
                               User.sec_pub_recieve,
                               User.message_push,
                               User.qq,
                               User.gender,
                               User.company_id,
                               UserResume.work_intention,
                               UserResume.work_history,
                               UserResume.age if UserResume.age  else '',
                               UserResume.education). \
            outerjoin(UserResume,
                      User.user_id == UserResume.user_id).filter(User.user_id == user_id).first()
        if q:
            return {
                "intention": q.work_intention,
                "exper": q.work_history,
                "user_id": q.user_id,
                "email": q.email,
                "age": q.age,
                "header_img": q.header_img,
                "openid": q.openid,
                "sec_pub_tel": q.sec_pub_tel,
                "sec_pub_recieve": q.sec_pub_recieve,
                "message_push": q.message_push,
                "qq": q.qq,
                "gender": q.gender,
                "company_id": q.company_id,
                "educate": q.education
            }
        else:
            return None

    # 1-普通用户 2-企业用户 3-中介用户
    def get_by_openid(self, openid, user_type=1):
        dao = UserDao(self.session)
        return dao.get_by_openid(openid, user_type=user_type)

    # 获得用户详情信息
    def get_detail(self, user_id):
        q = self.session.query(User,
                               User.user_id,
                               User.company_id,
                               User.wx_nick_name,
                               Company.name,
                               Company.certification,
                               Company.address,
                               Company.note).join(
            Company,
            User.company_id == Company.company_id,
            isouter=True) \
            .filter(User.user_id == user_id).first()
        if q:
            job_q = self.session.query(Job) \
                .filter(Job.create_userid == user_id)
            job_total = job_q.count()
            # 通过的数量
            ok_count = self.session.query(UserDeliverStatus). \
                filter(UserDeliverStatus.company_id == q.company_id) \
                .filter(UserDeliverStatus.audit_status == 1).count()
            return {
                'user_id': q.user_id,
                'company_id': q.company_id if q.company_id else '',
                'user_name': q.wx_nick_name,
                'company_name': q.name if q.name else '',
                'address': q.address if q.address else '',
                'note': q.note if q.note else '',
                'certification': q.certification if q.certification else '',
                'job_total': job_total,
                'ok_total': ok_count
            }
        else:
            return None


if __name__ == '__main__':
    # from utils.work_db import Session
    #
    # session = Session()
    # uservice = UserService(session)
    # data = uservice.get_detail(user_id=27)
    # print(data)
    pass
